
package com.yimixian.app.activity;

import com.squareup.picasso.Picasso;
import com.ta.utdid2.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.BuildConfig;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.AdvertInfo;
import com.yimixian.app.net.NetConstants;
import com.yimixian.app.rest.YmxDataService;
import com.yimixian.app.ui.dialog.WheelViewDialogNew;
import com.yimixian.app.ui.wheelview.WheelView;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.ymx.sdk.animator.animatorUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SplashActivity extends Activity {
    private static final long SPLASH_TIME_INTERVAL = DateUtils.SECOND_IN_MILLIS * 1;

    private ImageView adImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        adImg = (ImageView) findViewById(R.id.ad_img);
        // 清除上次的store
        SharedPreferencesHelper.clearStore(SharePreferenceKeys.CURRENT_STORE);
        // 初始化订单tab状态
        SharedPreferencesHelper.commitBoolean(Constants.ISUPDATEORDERTABRPOINT, true);
        final Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                AdvertInfo advertInfo = DataManager.getInstance().getAdInfo();
                String url = advertInfo == null ? null : advertInfo.url;
                if (!StringUtils.isEmpty(url)) {
                    showAdImage(mHandler, url);
                    return;
                }
                dealFinishAnimation();
            }
        }, SPLASH_TIME_INTERVAL);

    }

    public void showAdImage(Handler mHandler, String url) {
        loadAd(url);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dealFinishAnimation();
            }
        }, SPLASH_TIME_INTERVAL * 3);
    }

    /**
     * 加载广告图片
     *
     * @param url
     */
    public void loadAd(String url) {
        adImg.setVisibility(View.VISIBLE);
        Picasso picasso = Picasso.with(this);
        picasso.load(url).into(adImg);
        animatorUtils.alpha(adImg, 0, 1, 500, null).start();
    }

    public void dealFinishAnimation() {
        int versioncode = 0;
        try {
            versioncode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (BuildConfig.SERVER_ENVIRONMENT == Constants.ServerEnvironment.Mock) {
            showMutiplePackageSelect(versioncode);
            return;
        }
        startActivity(versioncode);
    }

    public void startActivity(int versioncode) {
        if (SharedPreferencesHelper.getVersionCode() != versioncode) {
            startActivity(GuideActivity.buildIntent(SystemFramework
                    .getInstance().getGlobalContext()));
            SharedPreferencesHelper.setVersionCode(versioncode);
        } else {
            startActivity(YMXMainActivity.buildIntent(SystemFramework
                    .getInstance().getGlobalContext()));
        }
        finishWithAnimation();
    }

    public void showMutiplePackageSelect(final int versioncode) {
        if (!isFinishing()) {
            final CharSequence[] charSequences = {
                    "RD", "TEST", "STAGING", "RELEASE"
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请选择:")
                    .setItems(charSequences, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String host = "";
                            YmxDataService.sInstance = null;
                            switch (which) {
                                case 0:
                                    Constants.MOCK_END_POINT = YmxDataService.RD_ENDPOINT;
                                    host = YmxDataService.RD_ENDPOINT;
                                    break;
                                case 1:
                                    Constants.MOCK_END_POINT = YmxDataService.TEST_ENDPOINT;
                                    host = YmxDataService.TEST_ENDPOINT;
                                    break;
                                case 2:
                                    Constants.MOCK_END_POINT = YmxDataService.STAGING_ENDPOINT;
                                    host = YmxDataService.STAGING_ENDPOINT;
                                    break;
                                case 3:
                                    Constants.MOCK_END_POINT = YmxDataService.RELEASE_ENDPOINT;
                                    host = YmxDataService.RELEASE_ENDPOINT;
                                    break;
                                default:
                                    Constants.MOCK_END_POINT = YmxDataService.RELEASE_ENDPOINT;
                                    host = YmxDataService.RELEASE_ENDPOINT;
                                    break;
                            }

                            host = host.substring(7);
                            NetConstants.HTTP_HOST = host.substring(0, host.length() - 1);

                            startActivity(versioncode);
                        }
                    }).show();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void finishWithAnimation() {
        this.finish();
        overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
    }
}
